/**
 * Created by lwy on 2017/8/17.
 */


$(function(){
    $.ajax({
        url:'/crm/shopCRMC/getAgentMessage.json',
        dataType:'json',
        data:{
            agentId:$('#idBox').html(),
            token:localStorage.getItem('crmtoken'),
        },
        type:'post',
        success:function(data){
            if(data.code==-1){
                window.parent.location.href="/crm/login.jsp"
            }
            if(data.code==0){
                alert(data.msg);
            }
            if(data.code==1){

                $('#id-record').empty();
                    var ifValid = '';
                    if(data.datas.valid){
                        ifValid = '是';
                    }else{
                        ifValid = '否';
                    }
                    $('<tr class="gradeA odd">'+
                        '<a href=""><td class="sorting_1 withdrawId" style="pointer-events: none;">'+data.datas.agentId+'</td></a>'+
                        '<td class=" ">'+data.datas.agentUserName+'</td>'+
                        '<td class=" ">'+data.datas.agencyType+'</td>'+
                        '<td class="center ">'+ifValid+'</td>'+
                        '<td class="sorting_1">'+data.datas.agentUserPhone+'</td>'+
                        '<td class=" ">'+data.datas.city+'</td>'+
                        '<td class=" ">'+data.datas.salesmanUserName+'</td>'+
                        '<td class=" ">'+data.datas.agentPerformance+'</td>'+
                        '<td class=" ">'+data.datas.agentShopNumber+'</td>'+
                        '</tr>').appendTo($('#id-record'));

            }
        }
    })
})

//可选择年月日
$('.layer-date').datetimepicker({
    language:  'zh-CN',
    minView: 'month',
    format: 'yyyy-mm-dd',
    autoclose: true,
    startView: 2,
    showMeridian: 1,
    pickerPosition: "bottom-left"
});


//个人收益
var page = 1;
var shopName = '',
    stateValue = '',
    code = '';
var data = new Date();
var dataTime = '';
var dataTime2 = '';
var year = data.getFullYear();
var month = data.getMonth()+1;
var day = data.getDate();
var today = year+'-'+month+'-'+day;
dataTime = year+'-'+month+'-'+day;
dataTime2 = year+'-'+month+'-'+day;
$('#datetimepicker').val(year+'-'+month+'-'+day);
$('#datetimepicker2').val(year+'-'+month+'-'+day);
var oneMonth = data.getMonth();
var thrMonth = data.getMonth()-2;
var oneYear = data.getFullYear()-1;
$('#today').click(function(){
    $(this).addClass('time-act');
    $(this).siblings().removeClass('time-act');
    $('#datetimepicker').val(year+'-'+month+'-'+day);
    $('#datetimepicker2').val(year+'-'+month+'-'+day);
    dataTime = year+'-'+month+'-'+day;
    dataTime2 = year+'-'+month+'-'+day;
});
$('#oneMonth').click(function(){
    $(this).addClass('time-act');
    $(this).siblings().removeClass('time-act');
    $('#datetimepicker').val(year+'-'+oneMonth+'-'+day);
    $('#datetimepicker2').val(year+'-'+month+'-'+day);
    dataTime = year+'-'+oneMonth+'-'+day;
    dataTime2 = year+'-'+month+'-'+day;
})
$('#thrMonth').click(function(){
    $(this).addClass('time-act');
    $(this).siblings().removeClass('time-act');
    $('#datetimepicker').val(year+'-'+thrMonth+'-'+day);
    $('#datetimepicker2').val(year+'-'+month+'-'+day);
    dataTime = year+'-'+thrMonth+'-'+day;
    dataTime2 = year+'-'+month+'-'+day;
})
$('#oneYear').click(function(){
    $(this).addClass('time-act');
    $(this).siblings().removeClass('time-act');
    $('#datetimepicker').val(oneYear+'-'+month+'-'+day);
    $('#datetimepicker2').val(year+'-'+month+'-'+day);
    dataTime = oneYear+'-'+month+'-'+day;
    dataTime2 = year+'-'+month+'-'+day;
})


function doAjax(){
    $.ajax({
        url:'/crm/shopCRMC/agentPerformance2.json',
        dataType:'json',
        data:{
            token:localStorage.getItem('crmtoken'),
            pageNu:page,
            shopName:shopName,
            smallMoney:$('#minMoney').val(),
            bigMoney:$('#maxMoney').val(),
            beginTime:$('#datetimepicker').val(),
            endTime:$('#datetimepicker2').val(),
            userName:$('#userName').val(),
            agentId:$('#idBox').html(),
        },
        type:'post',
        success: function (data) {
            if(data.code==-1){
                window.parent.location.href="/crm/login.jsp"
            }
            if(data.code==0){
                alert(data.msg);
            }
            if(data.code==1){
                $('#record').html(data.datas.recordsTotal);
                if(!data.datas.isShow){
                    $('.salesBox').hide();
                }else{
                    $('.salesBox').show();
                    $('#salesIncome').html(data.datas.saleManEarnings);
                    $('#salesRate').html(data.datas.saleManRate)
                }
                $('#record').html(data.datas.recordsTotal);
                $('#shopVolume').html(data.datas.shopEarnings);
                $('#myIncome').html(data.datas.agentEarnings);
                $('#myRate').html(data.datas.agentRate);
                $('#shopAccount').html(data.datas.shopCount);
                $('#withdraw-record').empty();
                for(var i=0;i<data.datas.shopList.length;i++){
                    var ifMain = '';
                    if(data.datas.shopList[i].isMain){
                        ifMain = '是';
                    }else{
                        ifMain = '否';
                    }
                    $('<tr class="gradeA odd">'+
                        '<td class="sorting_1 withdrawId" style="text-decoration: underline;cursor: pointer;">'+data.datas.shopList[i].id+'</td>'+
                        '<td class=" ">'+data.datas.shopList[i].shopName+'</td>'+
                        '<td class=" ">'+data.datas.shopList[i].agentMoney+'</td>'+
                        '<td class=" ">'+data.datas.shopList[i].saleManMoney+'</td>'+
                        '<td class=" ">'+data.datas.shopList[i].realName+'</td>'+
                        '<td class="center ">'+ifMain+'</td>'+
                        '</tr>').appendTo($('#withdraw-record'));
                }

                //id处理
                $('.withdrawId').click(function(){
                    var id = $(this).html();
                    window.open('/crm/shopCRMC/shopPerformance?shopId='+id);
                })

                $(".pagination").createPage({
                    totalPage:data.datas.totalPage,
                    currPage:data.datas.pageNu,
                    turndown:'true',
                    backFn:function(p){
                        page=p;
                        doAjax();
                    }
                });
            }
        }
    })
}

$(function(){
    doAjax();
})

//查询
$('#refer').click(function(){
    shopName = $('#shopName').val();
    code = $('#code').val();
    data = $('#datetimepicker').val();
    data2 = $('#datetimepicker2').val();
    page=1;
    doAjax();
})

